package com.jiukuaidao.zhulin.module.commodity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.base.BaseFragment;
import com.jiukuaidao.library.widget.smartlayout.SmartTabLayout;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.entity.ApiGeoCoderInfo;
import com.jiukuaidao.zhulin.entity.ApiHistoryAddressInfo;
import com.jiukuaidao.zhulin.entity.ApiSortCatList;
import com.jiukuaidao.zhulin.module.CommPagerAdapter;
import com.jiukuaidao.zhulin.module.commodity.contract.SortContract;
import com.jiukuaidao.zhulin.module.commodity.presenter.SortPresenter;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Author : H_Joker
 * Date : 2016/5/27
 * Description : 首页
 */
public class SortFragment extends BaseFragment<SortContract.Presenter> implements SortContract.View {

    @BindView(R.id.tl_sorttab)
    SmartTabLayout tlSorttab;
    @BindView(R.id.vp_pros)
    ViewPager vpPros;

    private List<Fragment> mFragments;
    private CommPagerAdapter mVpAdapter;

    @Override
    protected void bindMVP() {
        new SortPresenter(TaskProvider.provideSortRepository(Config.mContext), this);
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        initViewPager();
        loadCatList();
    }

    private void loadCatList() {
        String cityCode = AddressInfoUtil.getAddressInfo().cityCode;
        String latitude = AddressInfoUtil.getAddressInfo().latitude;
        String longitude = AddressInfoUtil.getAddressInfo().longitude;
        mPresenter.loadCatList(latitude, longitude, cityCode);
    }

    private void initViewPager() {
        mVpAdapter = new CommPagerAdapter(mFragments, getActivity().getSupportFragmentManager(), mPresenter.getCateNames());
        vpPros.setAdapter(mVpAdapter);
    }

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_sort;
    }

    /**
     * 定位当前位置 接收的地址
     *
     * @param apiGeoCoderInfo
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventLocationResult(ApiGeoCoderInfo apiGeoCoderInfo) {
        loadCatList();
    }

    /**
     * 地址切换 点击用户收货地址 接收的地址
     *
     * @param data
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventUserAddress(ApiHistoryAddressInfo.AddressInfo data) {
        loadCatList();
    }

    /**
     * 地址切换 点击搜索地址 接收的地址
     *
     * @param map
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventPlace(Map<String, String> map) {
        loadCatList();
    }

    @Override
    public void createFragments() {
        if (mFragments == null) {
            mFragments = new ArrayList<>();
        } else {
            mFragments.clear();
        }
        List<ApiSortCatList.ApiSortCateListItem> cateList = mPresenter.getCateList();
        for (ApiSortCatList.ApiSortCateListItem item : cateList) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("cateInfo", item);
            mFragments.add(SortProFragment.newInstance(SortProFragment.class, bundle));
        }
        mVpAdapter.setFragments(mFragments);
        mVpAdapter.setTitle(mPresenter.getCateNames());
        mVpAdapter.notifyDataSetChanged();
        tlSorttab.setViewPager(vpPros);
    }
}
